﻿Public Class FrmDeviceRestartView
    Private projectID As String

    Private Sub FrmDeviceRestartView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        projectID = MainForm.pi.ProjectID

        LoadDevices()

        dtBegin.Value = New DateTime(2000, 1, 1)
        dtEnd.Value = DateTime.Now.AddDays(1)
    End Sub

    Private Sub LoadDevices()
        Dim service As New DeviceRestartTimeService
        Dim lst As IList(Of DevTypeItem) = service.GeDevices(projectID)

        Me.chkDevicesList.Items.Clear()

        For i As Integer = 0 To lst.Count - 1
            Me.chkDevicesList.Items.Add(New FormatObject("{0}#设备", lst(i).NetId, lst(i)))
        Next
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        If (chkDevicesList.SelectedItems.Count < 1) Then
            MessageBox.Show("未选择要上传的设备")
            chkDevicesList.Focus()
            Return
        End If
        Dim idx As Integer = chkDevicesList.SelectedIndex

        Dim strFilter As String = " ProjectID='" + projectID + "' And  netID in ("
        For index As Integer = 0 To Me.chkDevicesList.SelectedItems.Count - 1
            Dim dev As DevTypeItem
            dev = CType(CType(Me.chkDevicesList.SelectedItems(index), FormatObject).Obj2, DevTypeItem)
            strFilter += String.Format("{0},", dev.NetId)
        Next

        'Me.chkDevicesList.SelectedItems.Clear()


        strFilter = strFilter.Remove(strFilter.Length - 1, 1)
        strFilter += " ) And "
        strFilter += String.Format(" RestartedTime between '{0}' and '{1}' ", dtBegin.Value.ToString("yyyy-MM-dd"), dtEnd.Value.ToString("yyyy-MM-dd"))

        Dim dts As New DeviceRestartTimeService()
 
        Dim lst As IList(Of DeviceRestartTime) = dts.Query(strFilter)


        tabMain.TabPages.Clear()

        GC.Collect()
        Dim dtFirst As DateTime
        If (lst.Count > 0) Then
            dtFirst = lst(0).FirstTime

            Dim lrs As IList(Of DeviceRestartTime) = New List(Of DeviceRestartTime)()
            Dim tab As CtlItemPowerShow

            For i As Integer = 0 To lst.Count - 1
                If (lst(i).FirstTime.CompareTo(dtFirst) = 0) Then
                    lrs.Add(lst(i))
                Else
                    tab = New CtlItemPowerShow(dtFirst.ToString("yyyy-MM-dd HH:mm:ss"), lrs)
                    tabMain.TabPages.Add(tab)
                    dtFirst = lst(i).FirstTime
                    lrs = New List(Of DeviceRestartTime)()
                    lrs.Add(lst(i))
                End If
            Next

            tab = New CtlItemPowerShow(dtFirst.ToString("yyyy-MM-dd HH:mm:ss"), lrs)

            tabMain.TabPages.Add(tab)

            tabMain.SelectTab(0)
        End If
        '   chkDevicesList.Focus()
        ' Me.chkDevicesList.SelectedIndex = idx
    End Sub

    Friend Class FormatObject

        Private fmt As String
        Public Obj1 As Object
        Public Obj2 As Object

        Sub New(ByVal format As String, ByVal obj1 As Object, ByVal obj2 As Object)
            fmt = format
            Me.Obj1 = obj1
            Me.Obj2 = obj2
        End Sub

        Public Overrides Function ToString() As String
            Return String.Format(fmt, Obj1)
        End Function
    End Class

 
End Class